#include "spi2.h"

#define BUFFER_SIZE 32

void SPI2_Init(void)
{
  gpio_init_type gpio_init_struct;
  spi_init_type spi_init_struct;
	
	crm_periph_clock_enable(CRM_SPI2_PERIPH_CLOCK, TRUE);
	crm_periph_clock_enable(SPI2_GPIO_CRM_CLK, TRUE);
	crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
	
	
	 /* master sck pin */
  gpio_init_struct.gpio_out_type       = GPIO_OUTPUT_PUSH_PULL;
  gpio_init_struct.gpio_pull           = GPIO_PULL_DOWN;
  gpio_init_struct.gpio_mode           = GPIO_MODE_MUX;
  gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  gpio_init_struct.gpio_pins           = SPI2_CLK_PIN;
  gpio_init(SPI2_GPIO, &gpio_init_struct);
  gpio_pin_mux_config(SPI2_GPIO, SPI2_CLK_PIN_SOURCE, SPI2_CLK_PIN_MUX_NUM);
	
	/* master mosi pin */
	//gpio_init_struct.gpio_mode				   = GPIO_MODE_MUX;
  gpio_init_struct.gpio_pull           = GPIO_PULL_UP;
  gpio_init_struct.gpio_pins           = SPI2_MOSI_PIN;
  gpio_init(SPI2_GPIO, &gpio_init_struct);
  gpio_pin_mux_config(SPI2_GPIO, SPI2_MOSI_PIN_SOURCE, SPI2_CLK_PIN_MUX_NUM);
	
	/* master miso pin */
	//gpio_init_struct.gpio_mode				   = GPIO_MODE_MUX;
  gpio_init_struct.gpio_pull           = GPIO_PULL_DOWN;
  gpio_init_struct.gpio_pins           = SPI2_MISO_PIN;
  gpio_init(SPI2_GPIO, &gpio_init_struct);
  gpio_pin_mux_config(SPI2_GPIO, SPI2_MISO_PIN_SOURCE, SPI2_CLK_PIN_MUX_NUM);
	
	
	/* master rst pin */
	gpio_init_struct.gpio_mode           = GPIO_MODE_OUTPUT;
  gpio_init_struct.gpio_pull           = GPIO_PULL_NONE;
  gpio_init_struct.gpio_pins           =  SPI2_CS_PIN;
  gpio_init(SPI2_GPIO, &gpio_init_struct);
	gpio_pin_mux_config(SPI2_GPIO, GPIO_PINS_SOURCE12, SPI2_CLK_PIN_MUX_NUM);

	


	gpio_bits_reset(SPI2_GPIO, SPI2_CS_PIN); //将CS拉低，OSD芯片有效
	gpio_bits_set(SPI2_GPIO, SPI2_CLK_PIN | SPI2_MOSI_PIN | SPI2_CLK_PIN);
	
  spi_default_para_init(&spi_init_struct);
  spi_init_struct.transmission_mode = SPI_TRANSMIT_FULL_DUPLEX; /*配置 SPI 为全双工发送模式*/
  spi_init_struct.master_slave_mode = SPI_MODE_MASTER;          /*配置 SPI 为主机模式*/
  spi_init_struct.mclk_freq_division = SPI_MCLK_DIV_16;          /*配置 SPI 时钟分频为 8 分频*/
  spi_init_struct.first_bit_transmission = SPI_FIRST_BIT_MSB;   /*配置 SPI 数据格式为 MSB */
  spi_init_struct.frame_bit_num = SPI_FRAME_8BIT;               /*配置 SPI 数据位数为每笔 8bit*/
  spi_init_struct.clock_polarity = SPI_CLOCK_POLARITY_LOW;      /*配置 SPI 时钟空闲时为高电平*/
  spi_init_struct.clock_phase = SPI_CLOCK_PHASE_1EDGE;          //第一个上升沿
  spi_init_struct.cs_mode_selection = SPI_CS_SOFTWARE_MODE;     /*配置 SPI 为软件 CS 管理模式*/
  
  spi_enable(SPI2, TRUE);
	spi_init(SPI2, &spi_init_struct);
}



//u8 SPI2_ReadWriteByte(u8 TxData)
//{		
//	u8 retry=0;				 	
//	while (spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET) //检查指定的SPI标志位设置与否:发送缓存空标志位
//		{
//		retry++;
//		if(retry>200)return 0;
//		}			  
//	spi_i2s_data_transmit(SPI2, TxData); //通过外设SPIx发送一个数据
//	retry=0;

//	while (spi_i2s_flag_get(SPI2, SPI_I2S_RDBF_FLAG) == RESET) //检查指定的SPI标志位设置与否:接受缓存非空标志位
//		{
//		retry++;
//		if(retry>200)return 0;
//		}	  						    
//	return spi_i2s_data_receive(SPI2); //返回通过SPIx最近接收的数据					    
//}

void delay_myms(uint16_t mxs)
{
	UINT16 j,p,ms=mxs*20;
	for(j=0; j<ms; j++)
	{
		for(p=0; p<ms; p++)
		{
			;
		}
	}
}

void write_at7456_addr_data(unsigned char addr, unsigned char dat)
{
	CS_Clr();
	//delay_myms(2);
	spi_i2s_data_receive(SPI2);
	while (spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET)
		;
		 spi_i2s_data_transmit(SPI2, addr); 
	while (spi_i2s_flag_get(SPI2, SPI_I2S_RDBF_FLAG) == RESET)
		; 
	 spi_i2s_data_receive(SPI2);
	while (spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET)
		;
	 spi_i2s_data_transmit(SPI2, dat); 
	while (spi_i2s_flag_get(SPI2, SPI_I2S_RDBF_FLAG) == RESET)
		; 
	 spi_i2s_data_receive(SPI2);
	while (spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET)
		;
	while (spi_i2s_flag_get(SPI2, SPI_I2S_BF_FLAG) != RESET)
			;
	CS_Set();

}

/*
unsigned char read_at7456_addr_data(unsigned char addr)
{
	unsigned char r;
		CS_Clr();
	addr |= 0x80;
	r = spi_i2s_data_receive(SPI2);
	while (spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET);
		 spi_i2s_data_transmit(SPI2, addr); 
	while (spi_i2s_flag_get(SPI2, SPI_I2S_RDBF_FLAG) == RESET); 
		 r = spi_i2s_data_receive(SPI2);
	while (spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET);
		 spi_i2s_data_transmit(SPI2, 0xff); 
	while (spi_i2s_flag_get(SPI2, SPI_I2S_RDBF_FLAG) == RESET); 
		 r = spi_i2s_data_receive(SPI2);
	while (spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET);
	while (spi_i2s_flag_get(SPI2, SPI_I2S_BF_FLAG) != RESET);
	CS_Set();
	return r;
	
//		unsigned char r;

//	CS_Clr();

//	addr |= 0x80;								// D7 = 1为读寄存器

//	r=spi_i2s_data_receive(SPI2);

//	// 等待发送缓冲器为空
//	while (spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET)
//		;
//	// 发送数据
//	spi_i2s_data_transmit(SPI2, addr); 
//	// 等待接收缓冲器为非空
//	while (spi_i2s_flag_get(SPI2, SPI_I2S_RDBF_FLAG) == RESET)
//		; 
//	r = spi_i2s_data_receive(SPI2);							// 读数据
//	while (spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET)
//		;
//	// 发送数据
//	spi_i2s_data_transmit(SPI2, 0xff); 
//	// 等待接收缓冲器为非空
//	while (spi_i2s_flag_get(SPI2, SPI_I2S_RDBF_FLAG) == RESET)
//		; 
//	r = spi_i2s_data_receive(SPI2);							// 读数据
//	while (spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET);
//	while (spi_i2s_flag_get(SPI2, SPI_I2S_BF_FLAG) != RESET);
//	CS_Set();

//	return r;
}
*/


unsigned char read_at7456_addr_data(unsigned char addr)
{
	unsigned char r;
		CS_Clr();
	//delay_myms(2);
	addr |= 0x80;
	r = spi_i2s_data_receive(SPI2);
	while (spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET)
		;
		 spi_i2s_data_transmit(SPI2, addr); 
	while (spi_i2s_flag_get(SPI2, SPI_I2S_RDBF_FLAG) == RESET)
		; 
		 r = spi_i2s_data_receive(SPI2);
	while (spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET)
		;
	while (spi_i2s_flag_get(SPI2, SPI_I2S_BF_FLAG) != RESET)
		;
	 CS_Set();
	return r;
}

unsigned char My_read_at7456_addr_data(unsigned char addr, unsigned char dat)
{
	unsigned char r;

	CS_Clr();

	addr |= 0x80;								// D7 = 1为读寄存器

	r=spi_i2s_data_receive(SPI2);

	// 等待发送缓冲器为空
	while (spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET)
		;
	// 发送数据
	spi_i2s_data_transmit(SPI2, addr); 
	// 等待接收缓冲器为非空
	while (spi_i2s_flag_get(SPI2, SPI_I2S_RDBF_FLAG) == RESET)
		; 
	r = spi_i2s_data_receive(SPI2);							// 读数据
	while (spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET)
		;
	// 发送数据
	spi_i2s_data_transmit(SPI2, 0xff); 
	// 等待接收缓冲器为非空
	while (spi_i2s_flag_get(SPI2, SPI_I2S_RDBF_FLAG) == RESET)
		; 
	r = spi_i2s_data_receive(SPI2);							// 读数据
	while (spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET);
	while (spi_i2s_flag_get(SPI2, SPI_I2S_BF_FLAG) != RESET);
	CS_Set();

	return r;
}
	
void write_at7456_data(unsigned char dat)
{
	CS_Clr();
     spi_i2s_data_receive(SPI2);
	while (spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET)
		;
		 spi_i2s_data_transmit(SPI2, dat); 
	while (spi_i2s_flag_get(SPI2, SPI_I2S_RDBF_FLAG) == RESET)
		; 
		 spi_i2s_data_receive(SPI2);
	while (spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET)
		;
	while (spi_i2s_flag_get(SPI2, SPI_I2S_BF_FLAG) != RESET)
		;
	CS_Set();
}

unsigned char read_at7456_data(void)
{
	unsigned char r;
	CS_Clr();
	   spi_i2s_data_receive(SPI2);
	while (spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET)
		;
		 spi_i2s_data_transmit(SPI2, 0xff); 
	while (spi_i2s_flag_get(SPI2, SPI_I2S_RDBF_FLAG) == RESET)
		; 
		 r = spi_i2s_data_receive(SPI2);
	while (spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET)
		;
	while (spi_i2s_flag_get(SPI2, SPI_I2S_BF_FLAG) != RESET);
		
	CS_Set();
	return r;

}

//uint8_t SPI2SendAndReceviceOneByte(uint8_t data){
//    while(spi_i2s_flag_get(SPI2, SPI_I2S_TDBE_FLAG) == RESET);
//    spi_i2s_data_transmit(SPI2, MAX7456ADD_STAT);
//    while(spi_i2s_flag_get(SPI2, SPI_I2S_RDBF_FLAG) == RESET);
//    return (uint8_t)spi_i2s_data_receive(SPI2);
//}
